<template>
  <view class="page">
    <myNavBar barIcon="back" title="实名认证"></myNavBar>
    <view class="mine-edit">
      <uni-forms ref="form" :modelValue="userInfo" label-position="right" label-width="30%">
        <uni-forms-item label="姓名:" name="realName">
          <uni-easyinput v-model="userInfo.realName" type="text" placeholder="请输入真实姓名" />
        </uni-forms-item>
        <uni-forms-item label="身份证号:" name="idCard">
          <uni-easyinput v-model="userInfo.idCard" type="text" placeholder="请输入身份证号" />
        </uni-forms-item>
      </uni-forms>
      <button type="primary" @click="onSubmit()">认证</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      userInfo: {
        realName: "",
        idCard: ""
      }
    };
  },
  components: {},
  created() {},
  methods: {
    async onSubmit() {
      // 1. 获取设备信息
      const metaInfo = uni.getFacialRecognitionMetaInfo();
      console.log("metaInfo============>", metaInfo);

      uniCloud.callFunction({
        name: "uni-cloud-verify",
        data: {
          step: 1,
          metaInfo,
          realName: this.userInfo.realName,
          idCard: this.userInfo.idCard
        },
        success({ result }) {
          console.log("result============>", result);
          // 3. 客户端调起sdk刷脸认证
          uni.startFacialRecognitionVerify({
            // 刷脸圈的颜色 加上这个参数会在ios端会提示 errCode: 10013 刷脸失败
            // progressBarColor: '#4a9f8b;',
            // 认证界面UI朝向 竖屏
            screenOrientation: "port",
            certifyId: result.data,
            success: async res => {
              console.log("res====>", res);
              const certResult = await uniCloud.callFunction({
                name: "uni-cloud-verify",
                data: {
                  step: 2,
                  certifyId: result.data
                }
              });

              console.log("certResult======================>", certResult);
              if (certResult.result.code == 200) {
                uni.showToast({
                  title: "认证成功",
                  icon: "none"
                });
                let data = {
                  realName: this.userInfo.realName,
                  idCard: this.userInfo.idCard
                };
                this.realauth(data);
              } else {
                uni.showToast({
                  title: "认证失败",
                  icon: "none"
                });
              }
            },
            fail() {
              uni.showToast({
                title: "认证失败",
                icon: "none"
              });
            }
          });
        }
      });
    },
    realauth(data) {
      this.$httpRequest({
        url: "/newUser/realNameAuthentication",
        method: "PUT",
        data: data
      }).then(res => {
        console.log("更新到咱们的数据库=============================>", res);
        setTimeout(() => {
          uni.navigateBack();
        }, 1000);
      });
    }
  },
  onLoad(options) {
    console.log("options==================>", options);
    this.userInfo.userId = options.id;
  }
};
</script>

<style scoped lang="scss">
.mine-edit {
  margin: 50rpx;

  .head-image {
    width: 200rpx;
    height: 200rpx;
  }
}

.radio {
  margin-right: 20rpx;
}
</style>